<template>
  <div class="department-container">
    <div class="app-container">
      <toolPage>
        <!-- <template #left>
          <h1>我是left</h1>
        </template>
        <template #right>
          <h1>我是right</h1>
        </template> -->
        <template #right>
          <el-button type="primary" size="mini" @click="handleDownload">导出</el-button>
          <el-button type="primary" size="mini" @click="$router.push('/employees/import')">导入</el-button>
          <el-button type="primary" size="mini" @click="addEmployee">+新增员工</el-button>
        </template>
      </toolPage>
      <!-- 新增员工对话框 -->
      <el-dialog
        title="添加"
        :visible.sync="dialogVisible"
        width="40%"
        :before-close="handleClose"
      >
        <!-- 表单组件 -->
        <empDialog ref="emp" @success="success" />
      </el-dialog>
      <!-- 员工列表渲染处 -->
      <el-card name="second">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号" width="150">
            <template v-slot="scope">
              {{ (employeeList.page-1)*employeeList.size+scope.$index+ 1 }}
            </template>
          </el-table-column>
          <el-table-column sortable prop="username" label="姓名" width="120" />
          <el-table-column sortable prop="mobile" label="手机号" width="120" />
          <el-table-column sortable prop="workNumber" label="工号" width="120" />
          <el-table-column
            sortable
            prop="formOfEmployment"
            label="聘用形式"
            width="120"
          >
            <template v-slot="scope">
              {{ scope.row.formOfEmployment===1? '正式':'非正式' }}
            </template>
          </el-table-column>
          <el-table-column
            sortable
            prop="departmentName"
            label="部门"
            width="120"
          />
          <el-table-column
            sortable
            label="入职时间"
            width="120"
          >
            <template v-slot="scope">
              {{ scope.row.timeOfEntry|forMat('YYYY-MM-DD hh:mm:ss') }}
            </template>
          </el-table-column>
          <el-table-column min-width="250px" fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="handleClick(scope.row)"
              >查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>

              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delHandler(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          name="second"
          :page-sizes="[2, 5, 7, 10]"
          :page-size="employeeList.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/emloyees'
import empDialog from './empDialog.vue'
export default {
  components: {
    empDialog
  },
  data() {
    return {
      tableData: [],
      employeeList: {
        page: 1,
        size: 10
      },
      total: null,
      dialogVisible: false

    }
  },
  created() {
    this.getEmployeeTable()
  },
  methods: {
    handleSizeChange(val) {
      this.employeeList.size = val
      // console.log(`每页 ${val} 条`)
      this.getEmployeeTable()
    },
    handleCurrentChange(val) {
      this.employeeList.page = val
      // console.log(`当前页: ${val}`)
      this.getEmployeeTable()
    },
    // 关闭对话框
    handleClose(done) {
      this.$refs.emp.close()
      done()
    },
    handleClick(row) {
      console.log(row)
    },
    // 获取员工列表
    async getEmployeeTable() {
      const res = await getEmployeeList(this.employeeList)
      // console.log(res)
      if (res.code === 10000) {
        this.tableData = res.data.rows
        if (res.data.rows.formOfEmployment === 1) {
          this.tableData.formOfEmployment = '正式'
        } else {
          this.tableData.formOfEmployment = '非正式'
        }
        this.total = res.data.total
      }
    },
    // 添加员工
    addEmployee() {
      // console.log(1)
      this.dialogVisible = true
    },
    // 成功后关闭对话框
    success() {
      this.dialogVisible = false
      this.getEmployeeTable()
    },
    // 删除员工
    async delHandler(row) {
      console.log(row.id)
      const res = await delEmployee(row.id).catch(e => e)
      // // console.log(res)
      if (res.code === 10000) {
        this.$message.success('删除数据成功!')
        this.getEmployeeTable()
        console.log(this.tableData)
        if (this.employeeList.page > 1 && this.tableData.length === 1) {
          this.employeeList.page--
        }
      }
    },
    // 文件导出
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const list = this.tableData // 一维数组

        const data = this.formatJson(list) // 二维数组

        excel.export_json_to_excel({
          header: this.tHeader, // 导出来的表头
          data, // 导出来的数据
          filename: this.filename, // 文件名
          autoWidth: true, // 宽度自适应
          bookType: 'xlsx' // 导出文件后缀类型
        })
        this.downloadLoading = false
      })
    },
    formatJson(list) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      this.tHeader = Object.values(map)
      const enkey = Object.keys(map)
      // console.log(this.tHeader)
      const obj = {}
      return list.map(item => {
        // console.log(item)
        enkey.forEach(key => {
          // console.log(key)
          obj[map[key]] = item[key]
        })
        const vals = Object.values(obj)
        return vals
      })
    }
  }
}
</script>
